# -*- coding:utf-8 -*-
"""
author:tuhou
time:2023/4/9 21:43 
思路：
"""
import execjs
import requests


result_url = "https://fuwu.nhsa.gov.cn/ebus/fuwu/api/nthl/api/CommQuery/queryServiceFacilities"
User_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"

with open('guojiayibao.js', 'r') as f:
    guojiayibao_js = execjs.compile(f.read())


def get_headers():
    """获取 header 参数，每次请求改变"""
    headers = guojiayibao_js.call("getHeaders")
    headers["User-Agent"] = User_agent
    headers["Content-Type"] = "application/json"
    headers["Host"] = "fuwu.nhsa.gov.cn"
    headers["Origin"] = "https://fuwu.nhsa.gov.cn"
    headers["Referer"] = "https://fuwu.nhsa.gov.cn/nationalHallSt/"
    headers["Content-Length"] = "413"
    return headers


def get_regn_code():
    """获取城市代码，返回结果无加密"""
    payload = {"data": {"transferFlag": ""}}
    response = requests.post(url=regn_code_url, json=payload, headers=get_headers())
    print(response.text)


def get_result():
    page_num = 3
    for page in range(1, int(page_num)+1):
        payload = {
            "addr": "",
            "regnCode": "110000",
            "medinsName": "",
            "sprtEcFlag": "",
            "medinsLvCode": "",
            "medinsTypeCode": "",
            "pageNum": page,
            "pageSize": 10
        }
        page += 1
        encrypted_payload = guojiayibao_js.call("get_encrypt", payload)
        encrypted_data = requests.post(url=result_url, json=encrypted_payload, headers=get_headers()).json()
        decrypted_data = guojiayibao_js.call("get_decrypt", encrypted_data)
        print(decrypted_data)


def main():
    get_result()


if __name__ == "__main__":
    main()